package com.alcj.controller;

import com.alcj.common.constant.SysConstant;
import com.alcj.common.framework.PageController;
import com.alcj.common.utils.CmnUtil;
import com.alcj.model.CarData;
import com.alcj.model.DetectReport;
import com.alcj.model.DetectReportDetail;
import com.alcj.model.User;
import com.alcj.service.CarDataService;
import com.alcj.service.DetectReportService;
import com.alibaba.fastjson.JSONObject;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @author :chenxh
 * @version :v1.0
 * @Package :com.alcj.controller
 * @Description :${TODO}(我的车况)
 * @date :
 */
@Controller
@RequestMapping("/myVehicle")
@SuppressWarnings("unchecked")
public class MyVehicleController extends PageController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    DetectReportService detectReportService;

    @Autowired
    private CarDataService carDataService;

    /**
     * @Author: chenxh
     * @Description:检测报告列表
     * @Date: 2017/9/7 16:13
     * @param: request
     * @param: model
     */
    @RequestMapping("/detectReport")
    public String getDetectReport(HttpServletRequest request, Model model) {
        try {
            // 登录操作用户ID
            User workUser = (User) request.getSession().getAttribute("User");
            if(CmnUtil.isEmpty(workUser)){
                return SysConstant.INDEX_PAGE;
            }
            // 获取当前选中车牌
            String vehicleId = request.getParameter("vehicleId");
            if(CmnUtil.isEmpty(vehicleId)){
                vehicleId = "";
            }
            //该用户的所有车辆
            CarData carData = new CarData();
            carData.setUser(workUser);
            List<CarData> listCarData = carDataService.findList(carData);

            DetectReport workDetectReport = new DetectReport();
            workDetectReport.setOperaid(workUser.getUserid());
            workDetectReport.setPlateNumber(vehicleId);
            // 获取页数
            String currentPage = request.getParameter("currentPage");
            // 设置页数
            if (CmnUtil.isEmpty(currentPage)) {
                currentPage = "1";
            }
            super.setCurrentPage(Integer.parseInt(currentPage));
            // 设置总件数数
            int total = detectReportService.getReprotCount(workDetectReport);
            super.giveTotalNum(total);
            if(total > 0){
                // 设置分页参数
                super.preparePageParams(workDetectReport);
                // 获取检测报告列表
                List<DetectReport> detectReportList = detectReportService.getReprotList(workDetectReport);
                model.addAttribute("reportList", detectReportList);
            }
            model.addAttribute("j_page", getPage());
            model.addAttribute("vehicleList", listCarData);
            model.addAttribute("vehicleId", vehicleId);
        } catch (Exception e) {
            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "detectReport";
    }

    /**
     * @Author: chenxh
     * @Description:检测报告
     * @Date: 2017/9/7 16:13
     * @param: request
     * @param: model
     */
    @RequestMapping("/reportDetail")
    public String getReportDetail(HttpServletRequest request, Model model) {
        try {
            // 登录操作用户ID
            User workUser = (User) request.getSession().getAttribute("User");
            if(CmnUtil.isEmpty(workUser)){
                return SysConstant.INDEX_PAGE;
            }
            // 获取页数
            String reportId = request.getParameter("id");
            DetectReport workDetectReport = new DetectReport();
            workDetectReport.setOperaid(workUser.getUserid());
            workDetectReport.setReportid(reportId);
            // 获取检测报告详细数据
            JSONObject detectReport = detectReportService.getReprotInfo(workDetectReport);
            List<DetectReportDetail> reportList = ( List<DetectReportDetail>)detectReport.get("reportList");
            model.addAttribute("report", (DetectReport)detectReport.get("report"));
            model.addAttribute("reportList", reportList);
            model.addAttribute("reportDetailList", (Map)detectReport.get("reportDetail"));
            model.addAttribute("lastList", (List)detectReport.get("lastList"));
            model.addAttribute("lastIndex", reportList.size());
        } catch (Exception e) {
            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "detectReportDetail";
    }

    /**
     * @Author: chenxh
     * @Description:车况分析
     * @Date: 2017/9/7 16:13
     * @param: request
     * @param: model
     */
    @RequestMapping("/vehicleState")
    public String getVehicleState(HttpServletRequest request, Model model) {
        try {
            // 登录操作用户ID
            User workUser = (User) request.getSession().getAttribute("User");
            if(CmnUtil.isEmpty(workUser)){
                return SysConstant.INDEX_PAGE;
            }
            // 获取当前选中车牌
            String vehicleId = request.getParameter("vehicleId");

            //该用户的所有车辆
            CarData carData = new CarData();
            carData.setUser(workUser);
            List<CarData> listCarData = carDataService.findList(carData);
            if(CmnUtil.isEmpty(listCarData) || listCarData.size() == 0){
                return "vehicleState";
            }
            if(CmnUtil.isEmpty(vehicleId)){
                // 默认选中第一辆车
                vehicleId = listCarData.get(0).getPlateNumber();
            }
            // 设定车况分析参数
            DetectReport workDetectReport = new DetectReport();
            workDetectReport.setOperaid(workUser.getUserid());
            workDetectReport.setPlateNumber(vehicleId);
            // 获取检测报告详细数据
            JSONObject detectReport = detectReportService.getVehicleInfo(workDetectReport);
            if(!CmnUtil.isEmpty(detectReport)){
                List<DetectReportDetail> reportList = ( List<DetectReportDetail>)detectReport.get("reportList");
                model.addAttribute("report", (DetectReport)detectReport.get("report"));
                model.addAttribute("reportList", reportList);
                model.addAttribute("reportDetailList", (Map)detectReport.get("reportDetail"));
                model.addAttribute("lastList", (Map)detectReport.get("lastList"));
                model.addAttribute("suggestList", (List)detectReport.get("suggestList"));
                model.addAttribute("lastIndex", reportList.size());
            }
            model.addAttribute("vehicleList", listCarData);
            model.addAttribute("vehicleId", vehicleId);
        } catch (Exception e) {
            // 异常报错
            logger.error("获取新闻资讯信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "vehicleState";
    }
}
